package com.stylefeng.guns.core.util;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.persistence.dao.ZyPictureMapper;
import com.stylefeng.guns.common.persistence.dao.ZyTeaMapper;
import com.stylefeng.guns.common.persistence.model.ZyPicture;
import com.stylefeng.guns.common.persistence.model.ZyTea;
import com.stylefeng.guns.core.page.PageBT;
import com.stylefeng.guns.core.page.PageInfoBT;
import com.stylefeng.guns.modular.fileManage.dao.ZyPictureDao;
import com.stylefeng.guns.modular.fileManage.dao.ZyTeaDao;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zengyongfeng
 * @description 用于分页查询的工具类
 */
@Component
public class PagingHandler {

    private static ZyTeaMapper teaMapper;
    private static ZyTeaDao teaDao;
    private static ZyPictureDao pictureDao;
    private static ZyPictureMapper pictureMapper;

    //表名
    static final String ZY_TEA = "zy_tea";
    static final String ZY_PICTURE = "zy_picture";

    /**
     * 分页查询的工具类
     *
     * @param tableName
     * @param pageParam
     * @return
     */
    public static PageInfoBT getPageResult(String tableName, PageBT pageParam) {
        Page page = new Page();
        PageInfoBT resutObject = new PageInfoBT(page);
        List rows;
        Integer totalCount;
        //根据指定的表名查询结果集
        if (ZY_TEA.equals(tableName)) {
            // 查询出总记录数
            totalCount = teaMapper.selectCount(Condition.create().eq("status", "1"));
            rows = teaDao.selectOnePage(pageParam);
        } else if (ZY_PICTURE.equals(tableName)) {
            totalCount = pictureMapper.selectCount(Condition.create());
            rows = pictureDao.selectOnePage(pageParam);
        } else {
            //传入的表不存在
            throw new BussinessException(BizExceptionEnum.TABLE_NAME_NOT_FOUND);
        }
        //查询结果集
        page.setTotal(totalCount);
        resutObject.setTotal(totalCount);
        resutObject.setRows(rows);
        return resutObject;
    }

    /**
     * 将关联的冲泡方式图片置顶查询
     *
     * @param pageParam
     * @return
     */
    public static PageInfoBT getPageResultByBrews(PageBT pageParam, Integer brewsId, Integer sequence) {
        Page page = new Page();
        PageInfoBT resutObject = new PageInfoBT(page);
        List rows;
        Integer totalCount;
        //根据指定的表名查询结果集
        totalCount = pictureMapper.selectCount(Condition.create());
        rows = pictureDao.selectOnePageByBrews(pageParam,brewsId,sequence);
        //查询结果集
        page.setTotal(totalCount);
        resutObject.setTotal(totalCount);
        resutObject.setRows(rows);
        return resutObject;
    }

    /**
     * 将关联的茶叶图片置顶查询
     *
     * @param pageParam
     * @return
     */
    public static PageInfoBT getPageResultByTea(PageBT pageParam, Integer teaId, String label) {
        Page page = new Page();
        PageInfoBT resutObject = new PageInfoBT(page);
        List rows;
        Integer totalCount;
        //根据指定的表名查询结果集
        totalCount = pictureMapper.selectCount(Condition.create());
        rows = pictureDao.selectOnePageByTea(pageParam,teaId,label);
        //查询结果集
        page.setTotal(totalCount);
        resutObject.setTotal(totalCount);
        resutObject.setRows(rows);
        return resutObject;
    }

    @Resource
    public void setPictureMapper(ZyPictureMapper pictureMapper) {
        PagingHandler.pictureMapper = pictureMapper;
    }

    @Resource
    public void setPictureDao(ZyPictureDao pictureDao) {
        PagingHandler.pictureDao = pictureDao;
    }

    @Resource
    public void setTeaMapper(ZyTeaMapper teaMapper) {
        PagingHandler.teaMapper = teaMapper;
    }

    @Resource
    public void setTeaDao(ZyTeaDao teaDao) {
        PagingHandler.teaDao = teaDao;
    }


}
